package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.cache.UserCacheInter;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.ReserveStatisFacade;
import com.qding.park.manager.pojo.ReserveParams;
import com.qding.park.manager.pojo.TbReserve;
import com.qding.park.manager.pojo.example.TbReserveExample;
import com.qding.park.manager.pojo.example.TbReserveExample.Criteria;
import com.qding.park.manager.service.statis.biz.ReserveBiz;
import com.qding.park.manager.service.statis.mapper.TbReserveStatisMapper;

@Service("reserveStatisFacade")
public class ReserveStatisFacadeImpl implements ReserveStatisFacade {
    private static final Logger _log = LoggerFactory.getLogger(ReserveStatisFacadeImpl.class);
	@Autowired
	private TbReserveStatisMapper tbReserveMapper;
	
	@Autowired
	private ReserveBiz reserveBiz;
	
	@Autowired
	private UserCacheInter userCacheInter;
	
	@Override
	public ParkDataResult orderInfoById(String orderId) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		TbReserve  reserve=tbReserveMapper.queryReservesInfoById(orderId);
		if(reserve!=null&&!StringUtils.isBlank(reserve.getId())){
			// 订单号
			map.put("orderId", reserve.getId());
			// 订单状态
			map.put("orderStatus", reserveBiz.getOrderStatusStr(reserve));
			// 手机号
			map.put("tel", userCacheInter.userId2tel(String.valueOf(reserve.getUserID())));
			// 车牌
			map.put("carNum", reserve.getCarNum());
			// 预定车场
			map.put("appointmentParkName", reserve.getParkName());
			// 计算押金
			reserve.setDeposit(reserveBiz.getOrderFee(reserve));
			// 预定押金
			map.put("appointmentFee", reserve.getDeposit());

			// 预定开始时间
			map.put("startTime", reserve.getStartTime() == null ? ""
					: DateUtils.formatDate(reserve.getStartTime(), DateUtils.DATE_FORMAT_DATETIME));
			// 预定结束时间
			map.put("endTime", reserve.getEndTime() == null ? ""
					: DateUtils.formatDate(reserve.getEndTime(), DateUtils.DATE_FORMAT_DATETIME));

			// 当前状态
			map.put("currentStatus", reserveBiz.getCurrentStatus(reserve));
		}
		
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		
		parkDataResult.putMapOnObj(map);
		return parkDataResult;
	}

    @Override
    public ParkDataResult reserveParams_set(ReserveParams params, int parkId) {
        reserveBiz.setReserveParams(params, parkId);
        ParkDataResult parkDataResult = ParkDataResult.createEmpty();
        return parkDataResult;
    }

    @Override
    public ParkDataResult list(TbReserveExample example, int jumpPage, int pageLength) {
        // 得到第一个统计条件
        Criteria criteria = example.getOredCriteria().get(0);
        // 过滤预定单
        criteria.andReserverTypeIsRes();
        
        //只选择有效的状态
        criteria.andEffectiveStatu();
        // 排序
        example.orderByCreateTimeDesc();

        PageHelper.startPage(jumpPage, pageLength);
        // 统计预定单
        List<TbReserve> reservelist = tbReserveMapper.reserveListByExample(example);

        PageInfo page = new PageInfo(reservelist);
        List dataList = new ArrayList();
        
        int totalFee=0;
        for (TbReserve reserve : reservelist) {
            
            Map<String, Object> map = new HashMap<String, Object>();
            // 订单号
            map.put("orderId", reserve.getId());
            // 订单状态
            map.put("orderStatus", reserveBiz.getOrderStatusStr(reserve));
            // 手机号
            map.put("tel", reserve.getTel());
            // 车牌
            map.put("carNum", reserve.getCarNum());
            // 预定车场
            map.put("appointmentParkName", reserve.getParkName());
            // 计算押金
//          reserve.setDeposit(reserveBiz.getOrderFee(reserve));
            // 预定押金
            map.put("appointmentFee", reserve.getDeposit());

            // 预定开始时间
            map.put("startTime", reserve.getStartTime() == null ? ""
                    : DateUtils.formatDate(reserve.getStartTime(), DateUtils.DATE_FORMAT_DATETIME));
            // 预定结束时间
            map.put("endTime", reserve.getEndTime() == null ? ""
                    : DateUtils.formatDate(reserve.getEndTime(), DateUtils.DATE_FORMAT_DATETIME));

            // 当前状态
            map.put("currentStatus", reserveBiz.getCurrentStatus(reserve));
            totalFee=totalFee+reserve.getDeposit();
            dataList.add(map);
        }
        
        ParkDataResult parkDataResult = ParkDataResult.createObjDate();
        parkDataResult.putDataOnObj("list", dataList);
        parkDataResult.putDataOnObj("totalFee", totalFee);
        parkDataResult.putDataOnObj("currentPage", page.getPageNum());
        parkDataResult.putDataOnObj("numPerPage", page.getPageSize());
        parkDataResult.putDataOnObj("totalCount", page.getTotal());
        parkDataResult.putDataOnObj("hast", page.isHasPreviousPage());
        parkDataResult.putDataOnObj("next", page.isHasNextPage());
        return parkDataResult;
    }

    @Override
    public ParkDataResult reserveParams(int parkId) throws BaseException {
        ReserveParams params = reserveBiz.queryReserveParams(parkId);
        if (params == null) {
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getDesc(), "没有找到对应的车场信息",
                            "parkId=" + parkId));
        }
        ParkDataResult parkDataResult = ParkDataResult.createObjDate();
        parkDataResult.putDataOnObj("appointmentTime", params.getAppointmentTime());
        parkDataResult.putDataOnObj("appointmentFee", params.getAppointmentFee());
        return parkDataResult;
    }

}
